/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.lidovky.articles.cli;

import cz.mzk.lidovky.articles.articleImport.Importer;
import cz.mzk.lidovky.articles.articlesData.ArticleDataParser;
import cz.mzk.lidovky.articles.domain.Article;
import cz.mzk.rehan.commons.PropertyLoader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Creates articles from cvs data file defined in properties.
 * Articles and relationships are possibly imported into Fedora repository.
 * Copies of foxmls and importLogs are stored into the proper directories.
 * (also defined in properties)
 * @author Martin Řehánek (rehan@mzk.cz)
 */
class ImportArticlesAction implements Action {

    private static final Logger logger = Logger.getLogger(ImportArticlesAction.class.getName());
    private final ArticleDataParser parser;
    private final PropertyLoader properties;
    private final boolean test;

    /**
     *
     * @param parser
     * @param properties
     * @param test if true the data won't be exported into Fedora but only created and save in foxmls and logs
     * @param simplified if true only input file for parsing is requied to be specified in properties
     * and output files and directories will be derived form it's name and created in the same directory
     */
    ImportArticlesAction(ArticleDataParser parser, PropertyLoader properties, boolean test, boolean simplified) {
        this.parser = parser;
        this.properties = properties;
        this.test = test;
    }

    @Override
    public void run() {
        try {
            List<Article> articles = processArticlesData(parser, true, false);
            logger.log(Level.INFO, "Articles to process: {0}", articles.size());
            Importer importer = Importer.instanceOf(properties);
            int counter = 0;
            for (Article article : articles) {
                String articlePid = null;
                if (test) {
                    articlePid = importer.saveArticleFoxmlAndLog(article);
                } else {
                    articlePid = importer.importArticle(article, true, true);
                }
                if (articlePid != null) {
                    counter++;
                }
            }
            logger.log(Level.INFO, "Articles processed: {0}", counter);
        } catch (Exception ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns correct articles, stores erroneous articles into file.
     * TODO: also store proper articles (into xml, create schema, create other implementation of ArticleDataParser)
     * @param properties
     * @param saveErroneousArticles if true the erroneous articles will be stored (filename defined in properties)
     * @param printStatistics
     * @return List of valid articles
     */
    private List<Article> processArticlesData(ArticleDataParser parser, boolean saveErroneousArticles, boolean printStatistics) {
        parser.parseArticles();
        if (saveErroneousArticles) {
            parser.saveErroneousArticles();
        }
        if (printStatistics) {
            parser.printStatistics(System.err);
        }
        return parser.getCorrectArticles();
    }
}
